package com.gxa.servlet.activity;

import com.fasterxml.jackson.databind.ObjectMapper;
import com.gxa.pojo.Result;
import com.gxa.service.avticity.ActivityService;
import com.gxa.service.avticity.impl.ActivityServiceImpl;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.HashMap;
import java.util.Map;

@WebServlet(urlPatterns = "/openAndCloseActivityType")
public class OpenAndCloseActivityTypeServlet extends HttpServlet {

    ObjectMapper objectMapper = new ObjectMapper();

    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {

        this.doPost(request,response);
    }

    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        request.setCharacterEncoding("UTF-8");
        response.setContentType("application/json;charset=UTF-8");
        ActivityService activityService = new ActivityServiceImpl();

        int atypeid = Integer.parseInt(request.getParameter("atypeid"));
        int state = Integer.parseInt(request.getParameter("state"));


        int i = activityService.openAndCloseActivityType(atypeid, state);
        PrintWriter writer = response.getWriter();
        Map<String,Object> map = new HashMap<>();
        String s= null;
        if (i>0){
            map.put("result", Result.ok("success"));
            s= objectMapper.writeValueAsString(map);
        }else {
            map.put("result",Result.error());
            s= objectMapper.writeValueAsString(map);
        }
        writer.write(s);
        writer.flush();
        writer.close();
    }
}
